package com.huqu.beans;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.apache.lucene.search.ScoreDoc;

import com.huqu.beans.sessionWeb.SessionWeb;
import com.huqu.entities.Page;
import com.huqu.entities.S100;
import com.huqu.utils.ApplicationConstant;
import com.huqu.utils.ApplicationHelper;
import com.huqu.utils.LuceneControl;

@ManagedBean(name = "searchBean")
@ViewScoped
public class SearchBean implements Serializable {
	private static final long serialVersionUID = -1230892124071961486L;

	@ManagedProperty("#{sessionWeb}")
	private SessionWeb sessionWeb;
	private String vLang = "VN";
	private Page page = new Page();

	private String keySearch = "";
	private String keySearchOld = "";
	private int totalResult = 0;
	List<S100> listSearch = new ArrayList<S100>();
	private double timework = 0;

	@PostConstruct
	public void init() {
		try {
			page.setOffset(1);
			page.setLimit(20);
			System.out.println("key-search= " + (String) ApplicationHelper.getSession(ApplicationConstant.KEY_SEARCH));
			if (ApplicationHelper.getSession(ApplicationConstant.KEY_SEARCH) != null) {
				keySearch = (String) ApplicationHelper.getSession(ApplicationConstant.KEY_SEARCH);
				if(keySearch.trim().length() > 0){
					actionSearch();
					ApplicationHelper.setSession(ApplicationConstant.KEY_SEARCH, "");
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// action tìm kiếm
	public void actionSearch() {
		try {
			if (keySearch.trim().length() > 0) {
				if (!keySearch.equals(keySearchOld)) {
					page.setOffset(1);
					page.setLimit(20);
					totalResult = 0;
					page.setTotalpage(totalResult, page.getLimit());
					keySearchOld = keySearch;
					listSearch = new ArrayList<S100>();
				}
				long beginTime = System.currentTimeMillis();
				ScoreDoc[] listScore = LuceneControl.searchIndex(keySearch,
						100000);
				totalResult = listScore.length;
				System.out.println("==========" + totalResult);
				listSearch = LuceneControl.getResultSearch(listScore,
						page.getOffset(), page.getLimit());
				page.setTotalpage(totalResult, page.getLimit());
				long endTime = System.currentTimeMillis();
				long time = (endTime - beginTime);
				timework = (double) time / 1000;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// phân trang
	public void goToPage() {
		try {
			System.out.println("--- GoToPage( )");
			Map<String, String> params = FacesContext.getCurrentInstance()
					.getExternalContext().getRequestParameterMap();
			int numberPage = Integer.parseInt(params.get("numberpage"));
			page.setOffset((numberPage - 1) * page.getLimit());
			page.setCurrentpage(numberPage);
			System.out.println("--- numberpage= " + numberPage);
			actionSearch();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public SessionWeb getSessionWeb() {
		return sessionWeb;
	}

	public void setSessionWeb(SessionWeb sessionWeb) {
		this.sessionWeb = sessionWeb;
	}

	public String getvLang() {
		return vLang;
	}

	public void setvLang(String vLang) {
		this.vLang = vLang;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public String getKeySearch() {
		return keySearch;
	}

	public void setKeySearch(String keySearch) {
		this.keySearch = keySearch;
	}

	public int getTotalResult() {
		return totalResult;
	}

	public void setTotalResult(int totalResult) {
		this.totalResult = totalResult;
	}

	public List<S100> getListSearch() {
		return listSearch;
	}

	public void setListSearch(List<S100> listSearch) {
		this.listSearch = listSearch;
	}

	public double getTimework() {
		return timework;
	}

	public void setTimework(double timework) {
		this.timework = timework;
	}

}
